package ar.com.sne.model.composer.per;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Row;
import org.zkoss.zul.api.Groupbox;

import foundation.common.util.BusinessException;
import foundation.zk.crud.composer.AbstractCRUDEditComposer;
import ar.com.sne.model.bo.geo.GeoCiudad;
import ar.com.sne.model.bo.geo.GeoTelefono;
import ar.com.sne.model.bo.per.PerCliente;
import ar.com.sne.model.bo.per.PerPersona;
import ar.com.sne.model.enumeration.per.TipoPersona;
import ar.com.sne.model.service.geo.GeoCiudadService;
import ar.com.sne.model.service.geo.GeoTelefonoService;
import ar.com.sne.model.service.per.PerClienteService;
import ar.com.sne.model.service.per.PerPersonaService;
import coop.tecso.foundation.common.macros.ComboBoxEntity;
import coop.tecso.foundation.common.macros.ComboBoxEnum;

@Service
@Scope("prototype")
public class EdicionClienteComposer extends AbstractCRUDEditComposer {

	private final Log log = LogFactory.getLog(getClass());

	@Resource
	private PerClienteService perClienteService;
	@Resource
	private PerPersonaService perPersonaService;
	@Resource
	private GeoCiudadService geoCiudadService;
	@Resource
	private GeoTelefonoService geoTelefonoService;
	
	private ComboBoxEnum cbbTipoEnvio;
	private ComboBoxEntity cbbGeoCiudad;
	private Groupbox gboxTelefonos;
	private Grid grdTelefonos;
	private List<GeoTelefono> telefonosList = new ArrayList<GeoTelefono>();
	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		GeoTelefono geoTelefono = new GeoTelefono();
		geoTelefono.setNumero("23232323");
		telefonosList.add(geoTelefono);
		if(this.getEditedObject().getId() != null){
//			this.setEditedObject(perClienteService.findById(this.getEditedObject().getId()));
//			telefonosList = this.getEditedObject().getPerPersonas().getGeoTelefonoses();
			
//			FinderFilter filter = new FinderFilter();
//			filter.addFilter("perPersona", getEditedObject().getPerPersonas());
//			telefonosList = geoTelefonoService.find(filter);
		}
		
		
	}
	
	public List<GeoCiudad> getGeoCiudadList(){
		return geoCiudadService.findAll();
	}
	
	public Object[] getTipoPersonaList(){
		return TipoPersona.values();
	}
	
	public PerCliente getEditedObject() {
		return (PerCliente) super.getEditedObject();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * coop.foundation.zk.crud.composer.AbstractCRUDEditComposer#updateObject
	 * (java.lang.Object)
	 */
	
	@Override
	protected void updateObject(Object objectToUpdate) {
		PerCliente perCliente = (PerCliente)objectToUpdate;
		perCliente.setActivado("1");
		PerPersona perPersona = perCliente.getPerPersonas();
		perPersona.setActivado("1");
		perPersonaService.save(perPersona);
		perClienteService.save(perCliente);
		if(telefonosList != null){
			for (GeoTelefono geoTelefono : telefonosList) {
				geoTelefono.setPerPersonas(perPersona);
				geoTelefonoService.save(geoTelefono);
			}
		}
		
	}

	
	@Override
	protected void verify(BusinessException be) {
		
		
	}
	
	public void onClick$addTelefonoButton(){
		log.info("entro");
		GeoTelefono geoTelefono = new GeoTelefono();
		geoTelefono.setNumero("23232323");
		telefonosList.add(geoTelefono);
		
		loadComponent(grdTelefonos);
		loadComponent(gboxTelefonos);
	}
	
	public void onClick$deleteButton(Event event){
		Row row = (Row)event.getTarget().getParent().getParent();
		GeoTelefono geoTel = (GeoTelefono)row.getValue();
		if(geoTel.getId() != null){
			geoTelefonoService.delete(geoTel);
		}else{
			telefonosList.remove(geoTel);
		}
		loadComponent(grdTelefonos);
		loadComponent(gboxTelefonos);
	}
	
	@Override
	protected Object getNewObjectToAdd() {
		return new PerCliente();
	}

	public List<GeoTelefono> getTelefonosList() {
		return telefonosList;
	}

	public void ListTelefonosList(List<GeoTelefono> telefonosList) {
		this.telefonosList = telefonosList;
	}


}